"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_promise_1 = __importDefault(require("fs-promise"));
const installDependencies_1 = __importDefault(require("../src/lib/installDependencies"));
const koa_1 = __importDefault(require("koa"));
const child_process_1 = require("child_process");
const colors_console_1 = __importDefault(require("colors-console"));
const configTool_1 = __importDefault(require("./com/configTool"));
const clientMiddleWare_1 = __importDefault(require("./middleware/clientMiddleWare"));
const webSocketTool_1 = __importDefault(require("./com/webSocketTool/webSocketTool"));
const portIsOccupied_1 = __importDefault(require("./lib/System/portIsOccupied"));
const Authentication_1 = __importDefault(require("./auth/Authentication"));
const plugMain_1 = __importDefault(require("../plug/plugMain"));
const MapResource_1 = __importDefault(require("./com/MapResource"));
const ws_1 = __importDefault(require("ws"));
const path_1 = __importDefault(require("path"));
const FSTool_1 = __importDefault(require("./lib/FSTool"));
console.log(`============ 程序启动中...${new Date().toLocaleString()} ============`);
const httpAPP = new koa_1.default();
async function main() {
    (0, child_process_1.exec)('reg add HKEY_CURRENT_USER\\Console /v QuickEdit /t REG_DWORD /d 00000000 /f');
    const initRes = await configTool_1.default.init();
    let endDate = new Date('2028-01-01').toISOString().split('T')[0];
    const serverId = await Authentication_1.default.getServerId();
    console.log((0, colors_console_1.default)('cyan', `【服务端设备唯一特征码】：${serverId}`));
    await (0, installDependencies_1.default)();
    process.on('uncaughtException', async (err) => {
        console.error('捕获到系统级的崩溃报错！！严重程度为高！，请排查', err && err.stack);
        const logPath = path_1.default.join(configTool_1.default.appBasePath, '/temp/logs/seriousError/nodeSeriousError.log');
        await fs_promise_1.default.ensureDir(path_1.default.dirname(logPath));
        const content = `${new Date().toLocaleString()} \n${String(err) + String(err.stack)} \n`;
        FSTool_1.default.writeText(logPath, content);
    });
    const httpServerPort = configTool_1.default.baseConfig.httpServerPort;
    if (httpServerPort !== await (0, portIsOccupied_1.default)(httpServerPort, false)) {
        console.log((0, colors_console_1.default)('red', `============ 端口号 ${httpServerPort} 被占用，程序启动失败 ============`));
        process.exit();
    }
    const ipAddr = configTool_1.default.baseConfig.localIPAddress;
    await new Promise((resolve, reject) => {
        const httpServer = httpAPP.listen(httpServerPort, async function () {
            console.log((0, colors_console_1.default)('yellow', `【静态资源网站资源目录】：${configTool_1.default.wwwPath}`));
            (0, clientMiddleWare_1.default)(httpAPP);
            const wss = new ws_1.default.Server({ server: httpServer });
            webSocketTool_1.default.start(wss);
            await (0, plugMain_1.default)();
            await MapResource_1.default.onWatcher();
            console.log((0, colors_console_1.default)('magenta', `【启动 HTTP  Web服务器】：URL:【http://${ipAddr}:${httpServerPort}】，与【WebSocket】共用【${httpServerPort}】端口`));
            console.log((0, colors_console_1.default)('green', `============ 系统启动成功！${configTool_1.default.baseConfig.name} 当前版本：${configTool_1.default.baseConfig.version} ============`));
            console.log((0, colors_console_1.default)('green', `============ 程序版本日期，2025-10-20 15:24，授权日期至：${endDate} ============`));
            resolve(null);
        });
    });
}
main().then();
